package com.uzhie.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 图片工具类
 */
public class ImageUtil {

    /**
     * 把多媒体上传的图片数据流,根据规则生成fileName,并写入磁盘进行存储
     * @param img 图片数据流
     * @param simNumber 车载sim卡号
     * @param id 多媒体信息上传消息id
     * @param channel 通道号
     * @param path 存储路径
     */
    public static void writeImage(byte[] img,String path, String simNumber,String id,String channel){
        try {
            String fileName =Tools.encodeID(simNumber,id,channel+"");
            writeImageToDisk(img,path,fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 存储文件到磁盘
     * @param img
     * @param path
     * @param fileName
     * @throws IOException
     */
    public static void writeImageToDisk(byte[] img,String path,String fileName) throws IOException {
        File file = new File(path + "\\" + fileName);
        FileOutputStream fops = new FileOutputStream(file);
        fops.write(img);
        fops.flush();
        fops.close();
        System.out.println("图片已经写入到D盘");
    }
}